package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description NursingPlanServiceImpl
 * @Author Peng Hongyan
 * @Date 2025-08-30
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlanList = nursingPlanMapper.getAll();
        List<NursingPlanVo> nursingPlanVoList = new ArrayList<>();
        if (nursingPlanList != null && !nursingPlanList.isEmpty()) {
            for (int i = 0; i < nursingPlanList.size(); i++) {
                NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlanList.get(i), NursingPlanVo.class);
                nursingPlanVoList.add(nursingPlanVo);
            }
            return nursingPlanVoList;
        }
        return null;
    }


    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    @Transactional
    public void addNursingPlan(NursingPlanVo nursingPlanVo) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        if (nursingPlanVo.getProjectPlans()!=null && ! nursingPlanVo.getProjectPlans().isEmpty()){
            List<NursingProjectPlan> nursingProjectPlanList = nursingPlanVo.getProjectPlans().stream().map(nursingProjectPlan -> {
                NursingProjectPlan nursingProjectPlan1 = new NursingProjectPlan();
                BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlan1);
                nursingProjectPlan1.setPlanId(Math.toIntExact(nursingPlan.getId()));
                return nursingProjectPlan1;
            }).collect(Collectors.toList());
            nursingProjectPlanMapper.insertNursingProjectPlanBatch(nursingProjectPlanList);
        }
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(Integer page, Integer pageSize, String planName, Integer status) {
        PageHelper.startPage(page, pageSize);
        Page<NursingPlan> byPage= nursingPlanMapper.getByPage(planName,status);

        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(byPage, NursingPlanVo.class);
        nursingPlanVoPageResponse.getRecords().forEach(
                np->{
                    np.setCount(
                            nursingLevelMapper.countByPlanId(np.getId())
                    );
                }
        );
        return nursingPlanVoPageResponse;
    }

    @Override
    public NursingPlanVo findNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.findNursingPlanById(id);
        List<NursingProjectPlan> projectPlans= nursingProjectPlanMapper.getByPlanId(Math.toIntExact(id));
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanVo nursingPlanVo) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        nursingProjectPlanMapper.deleteByPlanId(Math.toIntExact(nursingPlan.getId()));
        if (nursingPlanVo.getProjectPlans()!=null && ! nursingPlanVo.getProjectPlans().isEmpty()){
            //批量插入
            List<NursingProjectPlan> nursingProjectPlanList = nursingPlanVo.getProjectPlans().stream().map(nursingProjectPlan -> {
                NursingProjectPlan nursingProjectPlan1 = new NursingProjectPlan();
                BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlan1);
                nursingProjectPlan1.setPlanId(Math.toIntExact(nursingPlan.getId()));
                return nursingProjectPlan1;
            }).collect(Collectors.toList());
            nursingProjectPlanMapper.insertNursingProjectPlanBatch(nursingProjectPlanList);
        }

    }

    @Transactional
    @Override
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteNursingPlan(id);
        nursingProjectPlanMapper.deleteByPlanId(Math.toIntExact(id));
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
